package com.baidu.process.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 自定义线程池配置
 */
@Configuration
public class ThreadConfig {

    private int corePoolSize=8;
    private int maxPoolSize=8;
    private int keepAliveSeconds=80;
    private int queueCapacity=3;


    @Bean(name = "processSqlExecutorPool")
    public Executor createExecutor(){
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        //设置核心线程数
        threadPoolTaskExecutor.setCorePoolSize(corePoolSize);
        //最大线程数
        threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize);
        //允许线程的空闲时间
        threadPoolTaskExecutor.setKeepAliveSeconds(keepAliveSeconds);
        //缓冲队列数
        threadPoolTaskExecutor.setQueueCapacity(queueCapacity);
        //线程池名前缀
        threadPoolTaskExecutor.setThreadNamePrefix("process_sql_");
        //拒绝策略
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        return  threadPoolTaskExecutor;

    }



}
